/*
 * @Author: Drunk 1191047790@qq.com
 * @Date: 2024-10-24 11:06:03
 * @LastEditors: Drunk 1191047790@qq.com
 * @LastEditTime: 2024-11-07 09:52:53
 * @FilePath: \WWW\shopping\src\main.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */


import { createApp } from 'vue'
import App from './App.vue'

//加载路由
import router from './routers/index'
import rules from './routers/rules'

// 引入UI框架和样式
import 'vant/lib/index.css';
//导入全部组件
import Vant from 'vant';  

//引入自定义工具包
import tools from './services/tools'

//引入封装的异步请求axios
import request from './services/request'

//引入状态管理
import { createPinia } from 'pinia'; //引入pinia
import piniaPluginPersist from 'pinia-plugin-persist'//引入pinia数据持久化插件
import store from './store/index'


const app = createApp(App)
    .use(router)
    .use(request)
    .use(Vant)
    .use(createPinia().use(piniaPluginPersist)) //状态管理
    .use(store)
    .use(tools)
    .mount('#app')

// 路由守卫
router.beforeEach(async (to, from) => {
    
    //如果规则在里面就不需要登录
    if(rules.includes(to.path)) return true;

    //判断是否登录
    let {id, mobile} = app.$business.value

    var result = await app.$POST({
        url:'/business/check',
        params: {id, mobile}
    })

    if(!result) return false
    
    //当找不到用户的时候就要踢出去
    if(result.code == 0)
    {
        //重置缓存
        app.$store.$reset()

        app.$fail(result.msg)

        return {path:'/business/login'}
    }

    //覆盖一下缓存
    app.$business.value = result.data
    return true;//允许通过
})
